﻿using System.ComponentModel;

namespace AntdUI_HamburgerMenuTabs.Pages.Demo
{
    public partial class Theme : UserControl
    {
        public Theme()
        {
            InitializeComponent();

            Load += (s, e) =>
            {
                Init();
            };
        }

        private void Init()
        {
            btnLoadColours.Click += (s, e) =>
            {
                LoadColours();
            };

            inputSearch.TextChanged += (s, e) => Search();

            table1.AutoSizeColumnsMode = AntdUI.ColumnsMode.Fill;
            table1.Columns = [
                new AntdUI.Column("Name", "Name").SetColAlign().SetAlign(),
                new AntdUI.Column("ColorImg", "Color").SetColAlign().SetAlign(),
                new AntdUI.Column("Description", "Description"),
            ];
            LoadColours();

            table1.CellClick += (s, e) =>
            {
                if (e.Record is ColourModel record)
                {
                    if (e.RowIndex > 0)
                    {
                        if (table1.Columns[e.ColumnIndex].Key == "ColorImg")
                        {
                            UserControl uc = new()
                            {
                                Size = new(150, 50),
                                Padding = new Padding(0),
                            };
                            AntdUI.ColorPicker colorPicker = new()
                            {
                                Value = record.Color,
                                Dock = DockStyle.Fill,
                                WaveSize = 0,
                                ShowText = true,
                            };
                            colorPicker.ValueChanged += (s1, e1) =>
                            {
                                record.Color = e1.Value;
                                AntdUI.Style.Set(record.Colour, e1.Value);
                                ParentForm!.Refresh();
                            };
                            uc.Controls.Add(colorPicker);
                            AntdUI.Popover.open(new AntdUI.Popover.Config(table1, uc)
                            {
                                Offset = e.Rect,
                                AutoClose = 0
                            });
                        }
                    }
                }
            };
        }

        private void Search()
        {
            string kw = inputSearch.Text.Trim();
            if (kw == "")
            {
                table1.Binding(colours);
            }
            else
            {
                var data1 = colours.Where(x => x.Name?.Contains(kw, StringComparison.OrdinalIgnoreCase) == true).ToList();
                var data2 = new BindingList<ColourModel>(data1);
                table1.Binding(data2);
            }
        }

        private BindingList<ColourModel> colours = [];

        private void LoadColours()
        {
            table1.DataSource = null;

            colours = [
                new (AntdUI.Colour.Primary, AntdUI.Style.Db.Primary, "Primary", "品牌色"),
                new (AntdUI.Colour.PrimaryColor, AntdUI.Style.Db.PrimaryColor, "PrimaryColor", "文本颜色"),
                new (AntdUI.Colour.PrimaryHover, AntdUI.Style.Db.PrimaryHover, "PrimaryHover", "主色悬浮态（按钮、开关、复选框）"),
                new (AntdUI.Colour.PrimaryActive, AntdUI.Style.Db.PrimaryActive, "PrimaryActive", "主色激活态（按钮动画）"),
                new (AntdUI.Colour.PrimaryBg, AntdUI.Style.Db.PrimaryBg, "PrimaryBg", "主色背景色（按钮底部、下拉激活、文本框激活、菜单激活）"),
                new (AntdUI.Colour.PrimaryBgHover, AntdUI.Style.Db.PrimaryBgHover, "PrimaryBgHover", "主色背景悬浮态"),
                new (AntdUI.Colour.PrimaryBorder, AntdUI.Style.Db.PrimaryBorder, "PrimaryBorder", "主色的描边色"),
                new (AntdUI.Colour.PrimaryBorderHover, AntdUI.Style.Db.PrimaryBorderHover, "PrimaryBorderHover", "主色描边色悬浮态"),
                new (AntdUI.Colour.Success, AntdUI.Style.Db.Success, "Success", "成功色"),
                new (AntdUI.Colour.SuccessColor, AntdUI.Style.Db.SuccessColor, "SuccessColor", "文本颜色"),
                new (AntdUI.Colour.SuccessBg, AntdUI.Style.Db.SuccessBg, "SuccessBg", "成功色的背景颜色"),
                new (AntdUI.Colour.SuccessBorder, AntdUI.Style.Db.SuccessBorder, "SuccessBorder", "成功色的描边色"),
                new (AntdUI.Colour.SuccessHover, AntdUI.Style.Db.SuccessHover, "SuccessHover", "成功色的悬浮态"),
                new (AntdUI.Colour.SuccessActive, AntdUI.Style.Db.SuccessActive, "SuccessActive", "成功色的激活态"),
                new (AntdUI.Colour.Warning, AntdUI.Style.Db.Warning, "Warning", "警戒色"),
                new (AntdUI.Colour.WarningColor, AntdUI.Style.Db.WarningColor, "WarningColor", "文本颜色"),
                new (AntdUI.Colour.WarningBg, AntdUI.Style.Db.WarningBg, "WarningBg", "警戒色的背景颜色"),
                new (AntdUI.Colour.WarningBorder, AntdUI.Style.Db.WarningBorder, "WarningBorder", "警戒色的描边色"),
                new (AntdUI.Colour.WarningHover, AntdUI.Style.Db.WarningHover, "WarningHover", "警戒色的悬浮态"),
                new (AntdUI.Colour.WarningActive, AntdUI.Style.Db.WarningActive, "WarningActive", "警戒色的激活态"),
                new (AntdUI.Colour.Error, AntdUI.Style.Db.Error, "Error", "错误色"),
                new (AntdUI.Colour.ErrorColor, AntdUI.Style.Db.ErrorColor, "ErrorColor", "文本颜色"),
                new (AntdUI.Colour.ErrorBg, AntdUI.Style.Db.ErrorBg, "ErrorBg", "警戒色的背景颜色（按钮底部）"),
                new (AntdUI.Colour.ErrorBorder, AntdUI.Style.Db.ErrorBorder, "ErrorBorder", "警戒色的描边色"),
                new (AntdUI.Colour.ErrorHover, AntdUI.Style.Db.ErrorHover, "ErrorHover", "错误色的悬浮态"),
                new (AntdUI.Colour.ErrorActive, AntdUI.Style.Db.ErrorActive, "ErrorActive", "错误色的激活态"),
                new (AntdUI.Colour.Info, AntdUI.Style.Db.Info, "Info", "信息色"),
                new (AntdUI.Colour.InfoColor, AntdUI.Style.Db.InfoColor, "InfoColor", "文本颜色"),
                new (AntdUI.Colour.InfoBg, AntdUI.Style.Db.InfoBg, "InfoBg", "信息色的背景颜色（按钮底部）"),
                new (AntdUI.Colour.InfoBorder, AntdUI.Style.Db.InfoBorder, "InfoBorder", "信息色的描边色"),
                new (AntdUI.Colour.InfoHover, AntdUI.Style.Db.InfoHover, "InfoHover", "信息色的悬浮态"),
                new (AntdUI.Colour.InfoActive, AntdUI.Style.Db.InfoActive, "InfoActive", "信息色的激活态"),
                new (AntdUI.Colour.DefaultBg, AntdUI.Style.Db.DefaultBg, "DefaultBg", ""),
                new (AntdUI.Colour.DefaultColor, AntdUI.Style.Db.DefaultColor, "DefaultColor", ""),
                new (AntdUI.Colour.DefaultBorder, AntdUI.Style.Db.DefaultBorder, "DefaultBorder", ""),
                new (AntdUI.Colour.TagDefaultBg, AntdUI.Style.Db.TagDefaultBg, "TagDefaultBg", ""),
                new (AntdUI.Colour.TagDefaultColor, AntdUI.Style.Db.TagDefaultColor, "TagDefaultColor", ""),
                new (AntdUI.Colour.TextBase, AntdUI.Style.Db.TextBase, "TextBase", "基础文本色"),
                new (AntdUI.Colour.Text, AntdUI.Style.Db.Text, "Text", "一级文本色（菜单颜色、非激活下颜色、小清除按钮悬浮态）"),
                new (AntdUI.Colour.TextSecondary, AntdUI.Style.Db.TextSecondary, "TextSecondary", "二级文本色"),
                new (AntdUI.Colour.TextTertiary, AntdUI.Style.Db.TextTertiary, "TextTertiary", "三级文本色（小清除按钮）"),
                new (AntdUI.Colour.TextQuaternary, AntdUI.Style.Db.TextQuaternary, "TextQuaternary", "四级文本色（禁用色）"),
                new (AntdUI.Colour.BgBase, AntdUI.Style.Db.BgBase, "BgBase", "基础背景色"),
                new (AntdUI.Colour.BgContainer, AntdUI.Style.Db.BgContainer, "BgContainer", "组件的容器背景色 例如：默认按钮、输入框等。务必不要将其与 colorBgElevated 混淆。"),
                new (AntdUI.Colour.BgElevated, AntdUI.Style.Db.BgElevated, "BgElevated", "浮层容器背景色，在暗色模式下该 token 的色值会比 colorBgContainer 要亮一些。例如：模态框、弹出框、菜单等。"),
                new (AntdUI.Colour.BgLayout, AntdUI.Style.Db.BgLayout, "BgLayout", "该色用于页面整体布局的背景色，只有需要在页面中处于 B1 的视觉层级时才会使用该 token，其他用法都是错误的"),
                new (AntdUI.Colour.Fill, AntdUI.Style.Db.Fill, "Fill", "一级填充色"),
                new (AntdUI.Colour.FillSecondary, AntdUI.Style.Db.FillSecondary, "FillSecondary", "二级填充色（分页悬浮态、菜单悬浮态）"),
                new (AntdUI.Colour.FillTertiary, AntdUI.Style.Db.FillTertiary, "FillTertiary", "三级填充色（下拉悬浮态）"),
                new (AntdUI.Colour.FillQuaternary, AntdUI.Style.Db.FillQuaternary, "FillQuaternary", "四级填充色（幽灵按钮底部）"),
                new (AntdUI.Colour.BorderColor, AntdUI.Style.Db.BorderColor, "BorderColor", "边框颜色"),
                new (AntdUI.Colour.BorderSecondary, AntdUI.Style.Db.BorderSecondary, "BorderSecondary", ""),
                new (AntdUI.Colour.BorderColorDisable, AntdUI.Style.Db.BorderColorDisable, "BorderColorDisable", "禁用边框颜色"),
                new (AntdUI.Colour.Split, AntdUI.Style.Db.Split, "Split", "用于作为分割线的颜色，此颜色和 BorderSecondary 的颜色一致，但是用的是透明色"),
                new (AntdUI.Colour.HoverBg, AntdUI.Style.Db.HoverBg, "HoverBg", "选项悬浮态背景颜色"),
                new (AntdUI.Colour.HoverColor, AntdUI.Style.Db.HoverColor, "HoverColor", "选项悬浮态文本颜色"),
                new (AntdUI.Colour.SliderHandleColorDisabled, AntdUI.Style.Db.SliderHandleColorDisabled, "SliderHandleColorDisabled", ""),
            ];

            table1.Binding(colours);
        }

        class ColourModel : AntdUI.NotifyProperty
        {
            public ColourModel(AntdUI.Colour colour, Color color, string name, string description)
            {
                _colour = colour;
                _name = name;
                _description = description;
                _color = color;
                string svg = Common.Svg.GetColorBlock(_color);
                _colorImg = [new(svg)];
            }

            private AntdUI.Colour _colour;
            public AntdUI.Colour Colour
            {
                get => _colour;
                set
                {
                    _colour = value;
                    OnPropertyChanged();
                }
            }

            private Color _color;
            public Color Color
            {
                get => _color;
                set
                {
                    _color = value;
                    _colorImg = [new(Common.Svg.GetColorBlock(_color))];
                    OnPropertyChanged();
                }
            }

            private AntdUI.CellImage[]? _colorImg;
            public AntdUI.CellImage[]? ColorImg
            {
                get => _colorImg;
                set
                {
                    _colorImg = value;
                    OnPropertyChanged();
                }
            }

            private string _name;
            public string Name
            {
                get => _name;
                set
                {
                    _name = value;
                    OnPropertyChanged();
                }
            }

            private string _description;
            public string Description
            {
                get => _description;
                set
                {
                    _description = value;
                    OnPropertyChanged();
                }
            }
        }
    }
}